package com.umeox.watch.moto.launcher.utils;

import android.content.Context;
import android.util.Log;

import com.umeox.moto.common.utils.FileUtils;
import com.umeox.moto.watch.themes.util.ThemeUtils;
import com.umeox.watch.moto.launcher.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class SkinUtil {

    public static final String ASSETS_SKIN_DIR = "skin";
    public static final String ASSETS_MUSIC_DIR = "music";
    public static final String DEFAULT_SKIN = "default";

    public static synchronized void loadSkinResources(Context context) {
        final String[] skinNames = context.getResources().getStringArray(R.array.skin_name);
        for (String skinName : skinNames) {
            if (!DEFAULT_SKIN.equals(skinName)) {
                loadSkinResources(context, skinName);
            }
        }
    }

    public static void loadSkinResources(Context ctx, String skinName) {

        final File skinPath = ThemeUtils.getSkinPath();
        if (skinPath == null) {
            return;
        }
        final File mFaceApkFile = new File(skinPath, skinName);

        Log.d("skin", "loadSkinResources loading: " + skinName);
        try {
            FileOutputStream outputStream = new FileOutputStream(mFaceApkFile);
            final InputStream inputStream = ctx.getAssets().open(ASSETS_SKIN_DIR + File.separator + skinName);
            FileUtils.inputStreamToFile(inputStream, outputStream);

            Log.d("skin", "loadSkinResources finish: " + skinName);
        } catch (Exception e) {
            Log.e("skinUtil", "loadSkinResources: ", e);
        }
    }

    public static synchronized void loadMp3Resources(Context context) {

        final String[] musics = context.getResources().getStringArray(R.array.music_name);

        for (String music : musics) {
            Log.d("skin", "loadMp3Resources: " + music);
            loadMp3Resources(context, music);
        }
    }

    public static void loadMp3Resources(Context ctx, String musicName) {

        final File skinPath = ThemeUtils.getMusicPath();
        if (skinPath == null) {
            return;
        }
        final File mFaceApkFile = new File(skinPath, musicName);
        if (mFaceApkFile.exists()) {
            return;
        }

        Log.d("skin", "loadSkinResources loading: " + musicName);
        try {
            FileOutputStream outputStream = new FileOutputStream(mFaceApkFile);

            final InputStream inputStream = ctx.getAssets().open(ASSETS_MUSIC_DIR + File.separator + musicName);
            FileUtils.inputStreamToFile(inputStream, outputStream);

        } catch (Exception e) {
            Log.e("skin", "loadMp3Resources: ", e);
        }
    }
}
